﻿#region Copyright (c) 2000-2013 Developer Express Inc.
/*
{*******************************************************************}
{                                                                   }
{       Developer Express .NET Component Library                    }
{                                                                   }
{                                                                   }
{       Copyright (c) 2000-2013 Developer Express Inc.              }
{       ALL RIGHTS RESERVED                                         }
{                                                                   }
{   The entire contents of this file is protected by U.S. and       }
{   International Copyright Laws. Unauthorized reproduction,        }
{   reverse-engineering, and distribution of all or any portion of  }
{   the code contained in this file is strictly prohibited and may  }
{   result in severe civil and criminal penalties and will be       }
{   prosecuted to the maximum extent possible under the law.        }
{                                                                   }
{   RESTRICTIONS                                                    }
{                                                                   }
{   THIS SOURCE CODE AND ALL RESULTING INTERMEDIATE FILES           }
{   ARE CONFIDENTIAL AND PROPRIETARY TRADE                          }
{   SECRETS OF DEVELOPER EXPRESS INC. THE REGISTERED DEVELOPER IS   }
{   LICENSED TO DISTRIBUTE THE PRODUCT AND ALL ACCOMPANYING .NET    }
{   CONTROLS AS PART OF AN EXECUTABLE PROGRAM ONLY.                 }
{                                                                   }
{   THE SOURCE CODE CONTAINED WITHIN THIS FILE AND ALL RELATED      }
{   FILES OR ANY PORTION OF ITS CONTENTS SHALL AT NO TIME BE        }
{   COPIED, TRANSFERRED, SOLD, DISTRIBUTED, OR OTHERWISE MADE       }
{   AVAILABLE TO OTHER INDIVIDUALS WITHOUT EXPRESS WRITTEN CONSENT  }
{   AND PERMISSION FROM DEVELOPER EXPRESS INC.                      }
{                                                                   }
{   CONSULT THE END USER LICENSE AGREEMENT FOR INFORMATION ON       }
{   ADDITIONAL RESTRICTIONS.                                        }
{                                                                   }
{*******************************************************************}
*/
#endregion Copyright (c) 2000-2013 Developer Express Inc.

using UColor.Core.Helpers;
using System.Windows.Input;
using System.Windows;
using System.Windows.Media;
namespace UColor.WPF.Internal {
	public sealed class BaseControlToobar : BaseControlOrdinarPart {
		WeakEventHandler<ThePropertyChangedEventArgs<LString>> onParentModuleTitleChanged;
		WeakEventHandler<ThePropertyChangedEventArgs<bool>> onParentModuleAllowRtlChanged;
		WeakEventHandler<ThePropertyChangedEventArgs<Visibility>> onParentModuleSidebarVisibilityChanged;
		WeakEventHandler<ThePropertyChangedEventArgs<string>> onParentModuleSidebarTagChanged;
		WeakEventHandler<ThePropertyChangedEventArgs<ImageSource>> onParentModuleSidebarIconChanged;
		WeakEventHandler<ThePropertyChangedEventArgs<ImageSource>> onParentModuleSidebarIconSelectedChanged;
		WeakEventHandler<ThePropertyChangedEventArgs<DemoModuleControlOptionsView>> onParentOptionsViewChanged;
		WeakEventHandler<ThePropertyChangedEventArgs<DemoModuleControlView>> onParentDemoModuleViewChanged;
		WeakEventHandler<ThePropertyChangedEventArgs<bool>> onParentIsOptionsExpandedChanged;
		WeakEventHandler<ThePropertyChangedEventArgs<FlowDirection>> onParentDemoFlowDirectionChanged;
		LString title;
		DemoModuleControlView demoModuleView = DemoModuleControlView.Demo;
		DemoModuleControlOptionsView optionsView = DemoModuleControlOptionsView.Options;
		BaseControlMainPage parent;
		FlowDirection demoFlowDirection;
		bool hasOptions;
		bool allowThemes;
		bool demoButtonIsChecked = true;
		bool csCodeButtonIsChecked;
		bool vbCodeButtonIsChecked;
		bool xamlButtonIsChecked;
		bool optionsButtonIsChecked = true;
		bool themesButtonIsChecked;
		bool aboutButtonIsChecked;
		bool sidebarButtonIsChecked;
		bool updateModuleView = false;
		bool updateOptionsView = false;
		bool isOptionsExpanded;
		bool allowRtl = true;
		Visibility sidebarVisibility = Visibility.Collapsed;
		string sidebarTag;
		ImageSource sidebarIcon;
		ImageSource sidebarIconSelected;
		public BaseControlToobar(BaseControlMainPage parent, BaseControl demoBaseControl)
			: base(demoBaseControl) {
			onParentModuleTitleChanged = new WeakEventHandler<ThePropertyChangedEventArgs<LString>>(OnParentModuleTitleChanged);
			onParentModuleAllowRtlChanged = new WeakEventHandler<ThePropertyChangedEventArgs<bool>>(OnParentModuleAllowRtlChanged);
			onParentOptionsViewChanged = new WeakEventHandler<ThePropertyChangedEventArgs<DemoModuleControlOptionsView>>(OnParentOptionsViewChanged);
			onParentDemoModuleViewChanged = new WeakEventHandler<ThePropertyChangedEventArgs<DemoModuleControlView>>(OnParentDemoModuleViewChanged);
			onParentIsOptionsExpandedChanged = new WeakEventHandler<ThePropertyChangedEventArgs<bool>>(OnParentIsOptionsExpandedChanged);
			onParentDemoFlowDirectionChanged = new WeakEventHandler<ThePropertyChangedEventArgs<FlowDirection>>(OnParentDemoFlowDirectionChanged);
			onParentModuleSidebarVisibilityChanged = new WeakEventHandler<ThePropertyChangedEventArgs<Visibility>>(OnParentModuleSidebarVisibilityChanged);
			onParentModuleSidebarTagChanged = new WeakEventHandler<ThePropertyChangedEventArgs<string>>(OnParentModuleSidebarTagChanged);
			onParentModuleSidebarIconChanged = new WeakEventHandler<ThePropertyChangedEventArgs<ImageSource>>(OnParentModuleSidebarIconChanged);
			onParentModuleSidebarIconSelectedChanged = new WeakEventHandler<ThePropertyChangedEventArgs<ImageSource>>(OnParentModuleSidebarIconSelectedChanged);
			Parent = parent;
			Parent.SidebarVisibilityChanged += onParentModuleSidebarVisibilityChanged.Handler;
			Parent.SidebarTagChanged += onParentModuleSidebarTagChanged.Handler;
			Parent.SidebarIconChanged += onParentModuleSidebarIconChanged.Handler;
			Parent.SidebarIconSelectedChanged += onParentModuleSidebarIconSelectedChanged.Handler;
			OnParentModuleSidebarVisibilityChanged(Parent, new ThePropertyChangedEventArgs<Visibility>(Visibility.Collapsed, Parent.SidebarVisibility));
			OnParentModuleSidebarTagChanged(Parent, new ThePropertyChangedEventArgs<string>("Sidebar", Parent.SidebarTag));
			OnParentModuleSidebarIconChanged(Parent, new ThePropertyChangedEventArgs<ImageSource>(null, Parent.SidebarIcon));
			OnParentModuleSidebarIconSelectedChanged(Parent, new ThePropertyChangedEventArgs<ImageSource>(null, Parent.SidebarIconSelected));
			Parent.ModuleTitleChanged += onParentModuleTitleChanged.Handler;
			OnParentModuleTitleChanged(Parent, new ThePropertyChangedEventArgs<LString>(null, Parent.ModuleTitle));
			Parent.AllowRtlChanged += onParentModuleAllowRtlChanged.Handler;
			OnParentModuleAllowRtlChanged(Parent, new ThePropertyChangedEventArgs<bool>(true, Parent.AllowRtl));
			Parent.OptionsViewChanged += onParentOptionsViewChanged.Handler;
			OnParentOptionsViewChanged(Parent, new ThePropertyChangedEventArgs<DemoModuleControlOptionsView>(DemoModuleControlOptionsView.Options, Parent.OptionsView));
			Parent.DemoModuleViewChanged += onParentDemoModuleViewChanged.Handler;
			OnParentDemoModuleViewChanged(Parent, new ThePropertyChangedEventArgs<DemoModuleControlView>(DemoModuleControlView.Demo, Parent.DemoModuleView));
			Parent.IsOptionsExpandedChanged += OnParentIsOptionsExpandedChanged;
			Parent.DemoFlowDirectionChanged += onParentDemoFlowDirectionChanged.Handler;
			OnParentDemoFlowDirectionChanged(Parent, new ThePropertyChangedEventArgs<FlowDirection>(FlowDirection.LeftToRight, Parent.DemoFlowDirection));
			Initialized();
		}
		public BaseControlMainPage Parent {
			get { return parent; }
			private set { SetValue<BaseControlMainPage>("Parent", ref parent, value); }
		}
		public LString Title {
			get { return title; }
			set { SetValue<LString>("Title", ref title, value); }
		}
		public bool AllowRtl {
			get { return allowRtl; }
			set { SetValue<bool>("AllowRtl", ref allowRtl, value); }
		}
		public DemoModuleControlView DemoModuleView {
			get { return demoModuleView; }
			private set { SetValue<DemoModuleControlView>("DemoModuleView", ref demoModuleView, value, RaiseDemoModuleViewChanged); }
		}
		public DemoModuleControlOptionsView OptionsView {
			get { return optionsView; }
			private set { SetValue<DemoModuleControlOptionsView>("OptionsView", ref optionsView, value, RaiseOptionsViewChanged); }
		}
		public bool IsOptionsExpanded {
			get { return isOptionsExpanded; }
			private set { SetValue<bool>("IsOptionsExpanded", ref isOptionsExpanded, value, RaiseIsOptionsExpandedChanged); }
		}
		public bool DemoButtonIsChecked {
			get { return demoButtonIsChecked; }
			set { SetValue<bool>("DemoButtonIsChecked", ref demoButtonIsChecked, value, RaiseDemoButtonIsChecked); }
		}
		public bool CSCodeButtonIsChecked {
			get { return csCodeButtonIsChecked; }
			set { SetValue<bool>("CSCodeButtonIsChecked", ref csCodeButtonIsChecked, value, RaiseCSCodeButtonIsChecked); }
		}
		public bool VBCodeButtonIsChecked {
			get { return vbCodeButtonIsChecked; }
			set { SetValue<bool>("VBCodeButtonIsChecked", ref vbCodeButtonIsChecked, value, RaiseVBCodeButtonIsChecked); }
		}
		public bool XamlButtonIsChecked {
			get { return xamlButtonIsChecked; }
			set { SetValue<bool>("XamlButtonIsChecked", ref xamlButtonIsChecked, value, RaiseXamlButtonIsChecked); }
		}
		public bool OptionsButtonIsChecked {
			get { return optionsButtonIsChecked; }
			set { SetValue<bool>("OptionsButtonIsChecked", ref optionsButtonIsChecked, value, RaiseOptionsButtonIsCheckedChanged); }
		}
		public bool SidebarButtonIsChecked {
			get { return sidebarButtonIsChecked; }
			set { SetValue<bool>("SidebarButtonIsChecked", ref sidebarButtonIsChecked, value, RaiseSidebarButtonIsCheckedChanged); }
		}
		public bool ThemesButtonIsChecked {
			get { return themesButtonIsChecked; }
			set { SetValue<bool>("ThemesButtonIsChecked", ref themesButtonIsChecked, value, RaiseThemesButtonIsCheckedChanged); }
		}
		public bool AboutButtonIsChecked {
			get { return aboutButtonIsChecked; }
			set { SetValue<bool>("AboutButtonIsChecked", ref aboutButtonIsChecked, value, RaiseAboutButtonIsCheckedChanged); }
		}
		public FlowDirection DemoFlowDirection {
			get { return demoFlowDirection; }
			set { SetValue<FlowDirection>("DemoFlowDirection", ref demoFlowDirection, value, RaiseDemoFlowDirectionChanged); }
		}
		public bool HasOptions {
			get { return hasOptions; }
			set { SetValue<bool>("HasOptions", ref hasOptions, value); }
		}
		public Visibility SidebarVisibility {
			get { return sidebarVisibility; }
			set { SetValue<Visibility>("SidebarVisibility", ref sidebarVisibility, value); }
		}
		public string SidebarTag {
			get { return sidebarTag; }
			set { SetValue<string>("SidebarTag", ref sidebarTag, value); }
		}
		public ImageSource SidebarIcon {
			get { return sidebarIcon; }
			set { SetValue<ImageSource>("SidebarIcon", ref sidebarIcon, value); }
		}
		public ImageSource SidebarIconSelected {
			get { return sidebarIconSelected; }
			set { SetValue<ImageSource>("SidebarIconSelected", ref sidebarIconSelected, value); }
		}
		public bool AllowThemes {
			get { return allowThemes; }
			set { SetValue<bool>("AllowThemes", ref allowThemes, value); }
		}
		public event ThePropertyChangedEventHandler<FlowDirection> DemoFlowDirectionChanged;
		void OnParentModuleTitleChanged(object sender, ThePropertyChangedEventArgs<LString> e) {
			Title = e.NewValue;
		}
		void OnParentModuleAllowRtlChanged(object sender, ThePropertyChangedEventArgs<bool> e) {
			AllowRtl = e.NewValue;
		}
		void OnParentOptionsViewChanged(object sender, ThePropertyChangedEventArgs<DemoModuleControlOptionsView> e) {
			OptionsView = e.NewValue;
		}
		void OnParentDemoModuleViewChanged(object sender, ThePropertyChangedEventArgs<DemoModuleControlView> e) {
			DemoModuleView = e.NewValue;
		}
		void OnParentIsOptionsExpandedChanged(object sender, ThePropertyChangedEventArgs<bool> e) {
			IsOptionsExpanded = e.NewValue;
		}
		void OnParentDemoFlowDirectionChanged(object sender, ThePropertyChangedEventArgs<FlowDirection> e) {
			DemoFlowDirection = e.NewValue;
		}
		void OnParentModuleSidebarVisibilityChanged(object sender, ThePropertyChangedEventArgs<Visibility> e) {
			SidebarVisibility = e.NewValue;
		}
		void OnParentModuleSidebarTagChanged(object sender, ThePropertyChangedEventArgs<string> e) {
			SidebarTag = e.NewValue;
		}
		void OnParentModuleSidebarIconChanged(object sender, ThePropertyChangedEventArgs<ImageSource> e) {
			SidebarIcon= e.NewValue;
		}
		void OnParentModuleSidebarIconSelectedChanged(object sender, ThePropertyChangedEventArgs<ImageSource> e) {
			SidebarIconSelected = e.NewValue;
		}
		void RaiseDemoButtonIsChecked(bool oldValue, bool newValue) {
			if(updateModuleView) return;
			if(newValue)
				DemoModuleView = DemoModuleControlView.Demo;
		}
		void RaiseCSCodeButtonIsChecked(bool oldValue, bool newValue) {
			if(updateModuleView) return;
			if(newValue)
				DemoModuleView = DemoModuleControlView.CSCode;
		}
		void RaiseVBCodeButtonIsChecked(bool oldValue, bool newValue) {
			if(updateModuleView) return;
			if(newValue)
				DemoModuleView = DemoModuleControlView.VBCode;
		}
		void RaiseXamlButtonIsChecked(bool oldValue, bool newValue) {
			//if(updateModuleView) return;
			//if(newValue)
			//	DemoModuleView = DemoModuleControlView.Xaml;
            xamlButtonIsChecked = false;
            if(Parent.ModuleWrapper.CurrentDemoModule != null)
            {
                if(Parent.ModuleWrapper.CurrentDemoModule is UCOLOR.View.HomeView)
                {
                    UCOLOR.View.HomeView pview = Parent.ModuleWrapper.CurrentDemoModule as UCOLOR.View.HomeView;
                    UCOLOR.View.SearchView view = new UCOLOR.View.SearchView (pview.ProductCode,Parent.DemoBaseControl.ControlMainPart.PagesContainer.ModulesPage);
                    view.ShowDialog();
                    
                }
            }
            
            
		}
		void RaiseDemoModuleViewChanged(DemoModuleControlView oldValue, DemoModuleControlView newValue) {
			updateModuleView = true;
			DemoButtonIsChecked = false;
			CSCodeButtonIsChecked = false;
			VBCodeButtonIsChecked = false;
			XamlButtonIsChecked = false;
			switch(newValue) {
				case DemoModuleControlView.CSCode: CSCodeButtonIsChecked = true; break;
				case DemoModuleControlView.VBCode: VBCodeButtonIsChecked = true; break;
				case DemoModuleControlView.Xaml: XamlButtonIsChecked = true; break;
				default: DemoButtonIsChecked = true; break;
			}
			updateModuleView = false;
			Parent.DemoModuleView = newValue;
		}
		void RaiseOptionsButtonIsCheckedChanged(bool oldValue, bool newValue) {
			if(updateOptionsView) return;
			if(newValue)
				OptionsView = DemoModuleControlOptionsView.Options;
			IsOptionsExpanded = newValue;
		}
		void RaiseDemoFlowDirectionChanged(FlowDirection oldValue, FlowDirection newValue) {
            UCOLOR.View.CartView view = new UCOLOR.View.CartView();
            Parent.ModuleWrapper.LoadModule(view);
		}
		void RaiseThemesButtonIsCheckedChanged(bool oldValue, bool newValue) {
			if(updateOptionsView) return;
			if(newValue)
				OptionsView = DemoModuleControlOptionsView.Themes;
			IsOptionsExpanded = newValue;
		}
		void RaiseAboutButtonIsCheckedChanged(bool oldValue, bool newValue) {
			if(updateOptionsView) return;
			if(newValue)
				OptionsView = DemoModuleControlOptionsView.Description;
			IsOptionsExpanded = newValue;
		}
		void RaiseSidebarButtonIsCheckedChanged(bool oldValue, bool newValue) {
			if(updateOptionsView) return;
			if(newValue) 
				OptionsView = DemoModuleControlOptionsView.Sidebar;
			IsOptionsExpanded = newValue;
		}
		void RaiseOptionsViewChanged(DemoModuleControlOptionsView oldValue, DemoModuleControlOptionsView newValue) {
			updateOptionsView = true;
			OptionsButtonIsChecked = false;
			ThemesButtonIsChecked = false;
			AboutButtonIsChecked = false;
			SidebarButtonIsChecked = false;
			switch(newValue) {
				case DemoModuleControlOptionsView.Description: AboutButtonIsChecked = true; break;
				case DemoModuleControlOptionsView.Themes: ThemesButtonIsChecked = true; break;
				case DemoModuleControlOptionsView.Options: OptionsButtonIsChecked = HasOptions; break;
				case DemoModuleControlOptionsView.Sidebar: SidebarButtonIsChecked = SidebarVisibility == Visibility.Visible; break;
			}
			updateOptionsView = false;
			Parent.OptionsView = newValue;
		}
		void RaiseIsOptionsExpandedChanged(bool oldValue, bool newValue) {
			updateOptionsView = true;
			OptionsButtonIsChecked = false;
			ThemesButtonIsChecked = false;
			AboutButtonIsChecked = false;
			SidebarButtonIsChecked = false;
			if(newValue) {
				switch(OptionsView) {
					case DemoModuleControlOptionsView.Description: AboutButtonIsChecked = true; break;
					case DemoModuleControlOptionsView.Themes: ThemesButtonIsChecked = true; break;
					case DemoModuleControlOptionsView.Options: OptionsButtonIsChecked = true; break;
					case DemoModuleControlOptionsView.Sidebar: SidebarButtonIsChecked = true; break;
				}
			}
			updateOptionsView = false;
			Parent.IsOptionsExpanded = newValue;
		}
		#region Commands
		protected override void InitializeCommands() {
			base.InitializeCommands();
			RequestNextDemoCommand = new SimpleActionCommand(DoRequestNextDemo);
			RequestPrevDemoCommand = new SimpleActionCommand(DoRequestPrevDemo);
		}
		public ICommand RequestNextDemoCommand { get; private set; }
		public ICommand RequestPrevDemoCommand { get; private set; }
		void DoRequestNextDemo(object p) { Parent.RequestNextDemo(); }
		void DoRequestPrevDemo(object p) { Parent.RequestPrevDemo(); }
		#endregion
	}
}
